﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdCreateRectProfile.h"
#include "GmUiStructureCommandRegister.h"
#include "StructureCmdIds.h"
#include "IModelView.h"
#include "UiCommonDialog.h"
#include "GbmpModelViewTypes.h"
#include "IDocument.h"
#include "ILine3d.h"
#include "IModelLine.h"
#include "IUiView.h"
#include "DrawRectElementBehavior.h"
#include "GbmpUiPlatformUtils.h"
#include "IUserTransaction.h"
#include "IGraphicsElementShape.h"
#include "ProfileUsedModelLineUtils.h"
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

REGISTER_GM_COMMAND(CmdCreateRectProfile)

CmdCreateRectProfile::CmdCreateRectProfile()
    : CmdCreateRectElement(ID_CMD_STRUCTURE_DRAW_RECT_PROFILE)
{
}

bool CmdCreateRectProfile::OnExecuteCommand()
{
    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");
    const std::wstring& viewType = pModelView->GetUserDataId();

    if (PlanViewType != viewType && View3DViewType != viewType)
    {
        UiCommonDialog::ShowMessageBox(GBMP_TR(L"绘制轮廓线"), GBMP_TR(L"此视图下不支持绘制轮廓线"), (int)UiCommonDialog::ButtonType::OK);
        return false;
    }

    return true;
}

void CmdCreateRectProfile::SetupRibbonContextualPanel()
{
}

OwnerPtr<DrawRectElementBehavior> CmdCreateRectProfile::CreateDrawRectElementBehavior()
{
    OwnerPtr<DrawRectElementBehavior> opBehavior = NEW_AS_OWNER_PTR(DrawRectElementBehavior, UiDocumentViewUtils::GetCurrentDocument());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(opBehavior, L"opBehavior为空",L"GDMPLab",L"2024-03-30");
    opBehavior->SetOnFinishCallback(DrawRectElementBehavior::CreateOnFinishCallback(&CmdCreateRectProfile::OnRectFinish, this));
    opBehavior->SetOnPreviewCallback(DrawRectElementBehavior::CreateOnPreviewCallback(&CmdCreateRectProfile::OnRectPreview, this));

    return TransferOwnership(opBehavior);
}

bool CmdCreateRectProfile::CreatePresetElement(ElementId& elementId)
{
    elementId = ElementId::InvalidID;
    return false;
}

bool CmdCreateRectProfile::OnRectFinish(IUiView* pCurrentView, const Vector3d& firstPt, const Vector3d& cornerPt)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pCurrentView, L"pCurrentView为空",L"GDMPLab",L"2024-03-30");
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    IModelView* pModelView = pCurrentView->GetModelView();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> ut = IUserTransaction::Create(pDoc, GBMP_TR(L"绘制轮廓"));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(ut, L"事务创建失败",L"GDMPLab",L"2024-03-30");
    {
        // 根据对角线获取矩形的四个角点
        Vector3d rectCorners[4];
        bool bOk = GbmpUiPlatformUtils::GetRectCorners(pModelView, firstPt, cornerPt, rectCorners);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(bOk, L"根据对角线获取矩形的四个角点失败",L"GDMPLab",L"2024-03-30");
        // 根据四个角点绘制矩形轮廓线
        for (int i = 0; i < 4; ++i)
        {
            Vector3d startPt = rectCorners[i];
            Vector3d endPt = rectCorners[(i + 1) % 4];
            OwnerPtr<ILine3d> opLine = ILine3d::Create(startPt, endPt);
            ProfileUsedModelLineUtils::CreateModelLineUsedForProfile(pDoc, TransferOwnership(opLine));
        }
    }
    bool bCommit = ut->Commit();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(bCommit, L"事务提交失败",L"GDMPLab",L"2024-03-30");
    return true;
}

bool CmdCreateRectProfile::OnRectPreview(IUiView* pCurrentView, const Vector3d& firstPt, const Vector3d& pos, OwnerPtr<IGraphicsElementShape>& opPreviewGraphics)
{
    return false;
}
